//
//  XPBannerCell.swift
//  MTInternational
//
//  Created by xiaopin on 2020/12/28.
//  Copyright © 2020 scoresdata.net. All rights reserved.
//

import UIKit

class XPBannerView: XPBaseView, FSPagerViewDataSource, FSPagerViewDelegate {
    //MARK: 公开属性
    /// 数据源
    var dataSource:[XPBanner] = [XPBanner]()
    /// 点击banner事件
    var clickClosure:((XPBanner)->())?
    
    //MARK: 属性
    lazy var pagerView : FSPagerView  = {
       let pageView = FSPagerView()
        pageView.dataSource = self
        pageView.delegate = self
        pageView.automaticSlidingInterval = 3
        pageView.isInfinite = true
        pageView.register(XPBannerCell.self, forCellWithReuseIdentifier: "cell")
        pageView.automaticSlidingInterval = 3.0
        pageView.decelerationDistance = 1
        pageView.itemSize = FSPagerView.automaticSize
        pageView.layer.cornerRadius = 8.scale
        pageView.layer.masksToBounds = true
        return pageView
    }()
    lazy var pageControl: XPPageControl = {
        let pageControl = XPPageControl(frame: pp_frame(15, self.height - 20, self.width - 30, 20))
        pageControl.dotCornerRadius = 2
        pageControl.dotHeight = 4
        pageControl.dotSpace = 3
        pageControl.currentDotWidth = 12
        pageControl.otherDotWidth = 4
        pageControl.otherDotColor = #colorLiteral(red: 0.6980392157, green: 0.6980392157, blue: 0.6980392157, alpha: 1)
        pageControl.currentDotColor = #colorLiteral(red: 0.6980392157, green: 0.6980392157, blue: 0.6980392157, alpha: 1)
        return pageControl
    }()
    
    //MARK: 重载父类方法
    /// 初始化数据
    override func initData() {
        
    }
    
    /// 初始化当前视图的子控件
    override func initSubViews() {
        pagerView.beAddInto(self).snp.makeConstraints { m in
            m.top.left.bottom.right.equalToSuperview()
        }
        pageControl.beAddInto(self).snp.makeConstraints { m in
            m.height.equalTo(20.scale)
            m.left.bottom.right.equalToSuperview()
        }
        self.addSubview(pageControl)
    }
    
    //MARK: 自定义方法
    
    //MARK: 事件通知
    
    //MARK: 委托代理
    func numberOfItems(in pagerView: FSPagerView) -> Int {
        self.pageControl.numberOfPages = self.dataSource.count
        return self.dataSource.count
    }

    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> UICollectionViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index) as! XPBannerCell
        let banner = self.dataSource[index]
        cell.bgImage.loadImage(image_format_resizebywidth(banner.imageUrl, pp_screen_width) , placeholder: .rectangle)
        //cell.titleLabel.text = banner.title
        return cell
    }
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        pagerView.deselectItem(at: index, animated: true)
        pagerView.scrollToItem(at: index, animated: true)
        self.pageControl.currentPage = index
        let banner = self.dataSource[index]
        umengLogEvent("banner", label: banner.title ?? "")
        clickClosure?(banner)
    }

    func pagerViewDidScroll(_ pagerView: FSPagerView) {
       guard self.pageControl.currentPage != pagerView.currentIndex else {
           return
       }
       self.pageControl.currentPage = pagerView.currentIndex // Or Use KVO with property "currentIndex"
    }

}
